<?php

class DanhMucController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    private function getTaoDanhMucForm()
    {
        //tao form
                                        		$form = new Zend_Form ();
                                        		$form->setAction ( 'success' );
                                        		$form->setMethod ( 'post' );
                                        		$form->setDescription ( "Tạo danh mục" );
                                        		//add element
                                        		$form->addElement ( 'text', 'tenDM' );
                                        		$input_tenDM = $form->getElement ( 'tenDM' );
                                        		$input_tenDM->setLabel ( 'Tên danh mục:' );
                                        		$input_tenDM->setOrder ( 1 )->setRequired ( true );
                                        		
                                        		$form->addElement ( 'textarea', 'chuThich' );
                                        		$txt_chuThich = $form->getElement ( 'chuThich' );
                                        		$txt_chuThich->setLabel ( 'Chú thích:' );
                                        		$txt_chuThich->setOrder ( 2 );
                                        		
                                        		$form->addElement ( 'submit', 'submit' );
                                        		$submitButton = $form->getElement ( 'submit' );
                                        		$submitButton->setLabel ( 'Tạo danh mục!' );
                                        		$submitButton->setOrder ( 4 );
                                        		
                                        		//add vilidator
                                        		$input_tenDM->addValidator ( new Zend_Validate_StringLength ( 5, 30 ) );
                                        		//add fitter
                                        		$input_tenDM->addFilter ( new Zend_Filter_StringToLower () );
                                        		$input_tenDM->addFilter ( new Zend_Filter_StripTags () );
                                        		$txt_chuThich->addFilter ( new Zend_Filter_StripTags () );
                                        		return $form;
    }

    public function newAction()
    {
        // action body
                                        		//Add the form to the view
                                        		$form = $this->getTaoDanhMucForm ();
                                        		$this->view->form = $form;
    }

    public function successAction()
    {
        // action body
                $form = $this->getTaoDanhMucForm ();
                //check if form post
                if ($form->isValid ( $_POST )) {
                   $tenDM = $this->_request->getParam( 'tenDM' );
                   $chuThich = $this->_request->getParam( 'chuThich' );
                   //goi service
                   $danhMucService = Application_Model_service_DanhMucService::getInstance();
                   $result=$danhMucService->taoDanhMucSanPham ( $tenDM, $chuThich );
                   //add view
                   $this->view->result=$result;
                   $this->_helper->redirector('quanlydanhmuc');
                } 
                 else 
                 {
                    $this->view->error = $form->getMessages ();
                    $this->view->form = $form;}
    }

    public function deleteAction()
    {
        // action body
                                        $id=$this->_request->getParam('id');
                                        $DanhMucSV=Application_Model_service_DanhMucService::getInstance();
                                        $result=$DanhMucSV->xoaDanhMucSanPham($id);
                                        
                                        //addview
                                        $this->view->result=$result;
    }

    public function updateAction()
    {
        // action body
                                //lay thong tin cua danh muc can update
                                $id=$this->_request->getParam('id');
                                $danhMucSV=Application_Model_service_DanhMucService::getInstance();
                                $danhMuc=$danhMucSV->loadMotDanhMucSanPham($id);
                                //addView
                                $this->view->id=$danhMuc->getId();
                                $this->view->ten=$danhMuc->getTen();
                                $this->view->chuThich=$danhMuc->getChuThich();
    }

    public function sucessUpdateAction()
    {
        // action body
                        //get param
                        $id=$this->_request->getParam('id');
                        $ten=$this->_request->getParam('ten');
                        $chuThich=$this->_request->getParam('chuThich');
                        
                        //lay service
                        $service=Application_Model_service_DanhMucService::getInstance();
                        $result=$service->updateDanhMucSanPham($id,$ten,$chuThich);
                        
                        //add view
                        $this->view->result=$result;
                        $this->_helper->redirector('quanlydanhmuc');
    }

    public function loadAction()
    {
        // action body
                //goi service
                $service=Application_Model_service_DanhMucService::getInstance();
                $result=$service->layDanhSachDanhMucSanPham();
                
                //add view
                $this->view->list=$result;
                $this->_helper->layout()->disableLayout();
    }

    public function quanlydanhmucAction()
    {
        // action body
        // action body
                //goi service
                $mess=$this->_request->getParam('mess');
                $service=Application_Model_service_DanhMucService::getInstance();
                $result=$service->layDanhSachDanhMucSanPham();
                
                //add view
                $this->view->list=$result;
                if($mess!='') $this->_helper->layout()->disableLayout();
    }


}












